home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PatternEntry.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  282 lines

  1. /*
  2.  * @(#)PatternEntry.java    1.16 97/10/28
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30. package java.text;
  31.  
  32. import java.lang.Character;
  33.  
  34. /**
  35.  * Utility class for normalizing and merging patterns for collation.
  36.  * This is to be used with MergeCollation for adding patterns to an
  37.  * existing rule table.
  38.  * @see        MergeCollation
  39.  * @version    1.16 10/28/97
  40.  * @author     Mark Davis, Helena Shih
  41.  */
  42.  
  43. class PatternEntry {
  44.     /**
  45.      * Gets the current extension, quoted
  46.      */
  47.     public void appendQuotedExtension(StringBuffer toAddTo) {
  48.         appendQuoted(extension,toAddTo);
  49.     }
  50.  
  51.     /**
  52.      * Gets the current chars, quoted
  53.      */
  54.     public void appendQuotedChars(StringBuffer toAddTo) {
  55.         appendQuoted(chars,toAddTo);
  56.     }
  57.  
  58.     /**
  59.      * WARNING this is used for searching in a Vector.
  60.      * Because Vector.indexOf doesn't take a comparator,
  61.      * this method is ill-defined and ignores strength.
  62.      */
  63.     public boolean equals(Object obj) {
  64.         if (obj == null) return false;
  65.         PatternEntry other = (PatternEntry) obj;
  66.         boolean result = (chars.equals(other.chars) &&
  67.                           extension.equals(other.extension));
  68.         return result;
  69.     }
  70.  
  71.     /**
  72.      * For debugging.
  73.      */
  74.     public String toString() {
  75.         StringBuffer result = new StringBuffer();
  76.         addToBuffer(result, true, false, null);
  77.         return result.toString();
  78.     }
  79.  
  80.     /**
  81.      * Gets the strength of the entry.
  82.      */
  83.     int getStrength()
  84.     {
  85.     return strength;
  86.     }
  87.  
  88.     /**
  89.      * Gets the expanding characters of the entry.
  90.      */
  91.     String getExtension()
  92.     {
  93.         return extension;
  94.     }
  95.  
  96.     /**
  97.      * Gets the core characters of the entry.
  98.      */
  99.     String getChars()
  100.     {
  101.         return chars;
  102.     }
  103.  
  104.     // ===== privates =====
  105.  
  106.     PatternEntry() {
  107.     }
  108.  
  109.     PatternEntry(int strength,
  110.                  StringBuffer chars,
  111.                  StringBuffer extension)
  112.     {
  113.         this.strength = strength;
  114.         this.chars = chars.toString();
  115.         this.extension = extension.toString();
  116.     }
  117.  
  118.     void addToBuffer(StringBuffer toAddTo,
  119.                      boolean showExtension,
  120.                      boolean showWhiteSpace,
  121.                      PatternEntry lastEntry)
  122.     {
  123.         if (showWhiteSpace && toAddTo.length() > 0)
  124.             if (strength == Collator.PRIMARY || lastEntry != null)
  125.                 toAddTo.append('\n');
  126.             else
  127.                 toAddTo.append(' ');
  128.         if (lastEntry != null) {
  129.             toAddTo.append('&');
  130.             if (showWhiteSpace)
  131.                 toAddTo.append(' ');
  132.             lastEntry.appendQuotedChars(toAddTo);
  133.             appendQuotedExtension(toAddTo);
  134.             if (showWhiteSpace)
  135.                 toAddTo.append(' ');
  136.         }
  137.         switch (strength) {
  138.         case Collator.IDENTICAL: toAddTo.append('='); break;
  139.         case Collator.TERTIARY:  toAddTo.append(','); break;
  140.         case Collator.SECONDARY: toAddTo.append(';'); break;
  141.         case Collator.PRIMARY:   toAddTo.append('<'); break;
  142.         case RESET: toAddTo.append('&'); break;
  143.         case UNSET: toAddTo.append('?'); break;
  144.         }
  145.         if (showWhiteSpace)
  146.             toAddTo.append(' ');
  147.         appendQuoted(chars,toAddTo);
  148.         if (showExtension && extension.length() != 0) {
  149.             toAddTo.append('/');
  150.             appendQuoted(extension,toAddTo);
  151.         }
  152.     }
  153.  
  154.     int getNextEntry(String pattern,
  155.                      int i) throws ParseException
  156.     {
  157.         int newStrength = UNSET;
  158.         StringBuffer newChars = new StringBuffer();
  159.         StringBuffer newExtension = new StringBuffer();
  160.         boolean inChars = true;
  161.         boolean inQuote = false;
  162.  mainLoop:
  163.         while (i < pattern.length()) {
  164.             char ch = pattern.charAt(i);
  165.             if (inQuote) {
  166.                 if (ch == '\'') {
  167.                     inQuote = false;
  168.                 } else {
  169.                     if (newChars.length() == 0) newChars.append(ch);
  170.                     else if (inChars) newChars.append(ch);
  171.                     else newExtension.append(ch);
  172.                 }
  173.             } else switch (ch) {
  174.             case '=': if (newStrength != UNSET) break mainLoop;
  175.                 newStrength = Collator.IDENTICAL; break;
  176.             case ',': if (newStrength != UNSET) break mainLoop;
  177.                 newStrength = Collator.TERTIARY; break;
  178.             case ';': if (newStrength != UNSET) break mainLoop;
  179.                 newStrength = Collator.SECONDARY; break;
  180.             case '<': if (newStrength != UNSET) break mainLoop;
  181.                 newStrength = Collator.PRIMARY; break;
  182.             case '&': if (newStrength != UNSET) break mainLoop;
  183.                 newStrength = RESET; break;
  184.             case '\t':
  185.             case '\n':
  186.             case '\f':
  187.             case '\r':
  188.             case ' ': break; // skip whitespace TODO use Character
  189.             case '/': inChars = false; break;
  190.             case '\'':
  191.                 inQuote = true;
  192.                 ch = pattern.charAt(++i);
  193.                 if (newChars.length() == 0) newChars.append(ch);
  194.                 else if (inChars) newChars.append(ch);
  195.                 else newExtension.append(ch);
  196.                 break;
  197.             default:
  198.                 if (newStrength == UNSET) {
  199.                     throw new ParseException
  200.                         ("missing char (=,;<&) : " +
  201.                          pattern.substring(i,
  202.                             (i+10 < pattern.length()) ?
  203.                              i+10 : pattern.length()),
  204.                          i);
  205.                 }
  206.                 if (PatternEntry.isSpecialChar(ch) && (inQuote == false))
  207.                     throw new ParseException
  208.                         ("Unquoted punctuation character : " + Integer.toString(ch, 16), i);
  209.                 if (inChars) {
  210.                     newChars.append(ch);
  211.                 } else {
  212.                     newExtension.append(ch);
  213.                 }
  214.                 break;
  215.             }
  216.             i++;
  217.         }
  218.         if (newStrength == UNSET)
  219.             return -1;
  220.         if (newChars.length() == 0) {
  221.             throw new ParseException
  222.                 ("missing chars (=,;<&): " +
  223.                   pattern.substring(i,
  224.                       (i+10 < pattern.length()) ?
  225.                        i+10 : pattern.length()),
  226.                  i);
  227.         }
  228.         strength = newStrength;
  229.         chars = newChars.toString();
  230.         extension = newExtension.toString();
  231.         return i;
  232.     }
  233.  
  234.     static boolean isSpecialChar(char ch) {
  235.         return (((ch <= '\u002F') && (ch >= '\u0020')) ||
  236.                 ((ch <= '\u003F') && (ch >= '\u003A')) ||
  237.                 ((ch <= '\u0060') && (ch >= '\u005B')) ||
  238.                 ((ch <= '\u007E') && (ch >= '\u007B')));
  239.     }
  240.  
  241.     static void appendQuoted(String chars, StringBuffer toAddTo) {
  242.         boolean inQuote = false;
  243.         char ch = chars.charAt(0);
  244.         if (Character.isSpaceChar(ch)) {
  245.             inQuote = true;
  246.             toAddTo.append('\'');
  247.         } else {
  248.           if (PatternEntry.isSpecialChar(ch)) {
  249.                 inQuote = true;
  250.                 toAddTo.append('\'');
  251.             } else {
  252.                 switch (ch) {
  253.                     case 0x0010: case '\f': case '\r':
  254.                     case '\t': case '\n':  case '@':
  255.                     inQuote = true;
  256.                     toAddTo.append('\'');
  257.                     break;
  258.                 case '\'':
  259.                     inQuote = true;
  260.                     toAddTo.append('\'');
  261.                     break;
  262.                 default:
  263.                     if (inQuote) {
  264.                         inQuote = false; toAddTo.append('\'');
  265.                     }
  266.                     break;
  267.                 }
  268.            }
  269.         }
  270.         toAddTo.append(chars);
  271.         if (inQuote)
  272.             toAddTo.append('\'');
  273.     }
  274.  
  275.     static final int RESET = -2;
  276.     static final int UNSET = -1;
  277.  
  278.     int strength = UNSET;
  279.     String chars = "";
  280.     String extension = "";
  281. }
  282.